package com.dianxun.pagerecyclerviewdemo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MyAdapter extends RecyclerView.Adapter< MyAdapter.MyViewHolder > {

    public static List< String > data = new ArrayList<>();

    static {
        for ( int i = 1; i <= 15; i++ ) {
            data.add( i + "" );
        }
    }


    @Override
    public MyViewHolder onCreateViewHolder ( ViewGroup parent, int viewType ) {
        LayoutInflater inflater = LayoutInflater.from( parent.getContext() );
        View view = inflater.inflate( R.layout.layout_item, parent, false );
        return new MyViewHolder( view );
    }

    @Override
    public void onBindViewHolder ( final MyViewHolder holder, final int position ) {
        final String title = data.get( position );
        holder.tv_title.setText( title );
        holder.itemView.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick ( View v ) {
                Toast.makeText( v.getContext(), "item" + title + " 被点击了", Toast.LENGTH_SHORT ).show();
                holder.tv_title.setText( "G " + title );
                notifyItemChanged( position );
            }
        } );
    }

    @Override
    public int getItemCount () {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_title;

        public MyViewHolder ( View itemView ) {
            super( itemView );
            tv_title = (TextView) itemView.findViewById( R.id.tv_title );
        }
    }
}
